﻿Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Reporting.WebForms
Imports log4net

Partial Class Report_RP06_1
    Inherits System.Web.UI.Page

    Dim Report As Object
    Public ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("Report_RP06_1")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session("users") Is Nothing Then
            ScriptText = "<script> wclose();</script>"
        End If
    End Sub
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        'If Session("fundcenterS") = "" Then
        '    Session("fundcenterS") = "0011"
        '    Session("fundcenterE") = "0012"

        'End If

        'If Session("users") = "" Then
        '    Session("users") = "wassana"
        'End If
        'Session("year") = 2556
        'Session("version") = 2

        If Session("users") Is Nothing Then
            Response.Redirect("../LoginForm.aspx")
        End If


        Getdata()


    End Sub


    Function Getdata() As Decimal


        Dim adapter As New Data_ReportTableAdapters.R06TableAdapter
        Dim sql As String
        Dim dt As New Data_Report.R06DataTable

        sql = "  select CASE WHEN parent1  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent1 END AS parent1 "
        sql += ",CASE WHEN parent1  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent1_name END AS parent1_name "
        sql += ",CASE WHEN parent2  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent2 END AS parent2 "
        sql += ",CASE WHEN parent2  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent2_name END AS parent2_name"
        sql += ",CASE WHEN parent3  NOT IN (" & Session("FundcenterAll") & ")THEN NULL ELSE parent3 END AS parent3 "
        sql += ",CASE WHEN parent3  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent3_name END AS parent3_name"
        sql += ",CASE WHEN parent4  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent4 END AS parent4 "
        sql += ",CASE WHEN parent4  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent4_name END AS parent4_name"
        sql += ",CASE WHEN parent5  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent5 END AS parent5 "
        sql += ",CASE WHEN parent5  NOT IN (" & Session("FundcenterAll") & ") THEN NULL ELSE parent5_name END AS parent5_name"
        sql += ",Assetgroup,"
        sql += "  (SELECT AppConfigs.Description FROM AppConfigs WHERE AppConfigs.Name = 'ASSET_GROUP' AND AppConfigs.Value = ActivityTypes.AssetGroup)as AssetGroupName ,"
        sql += "  ActivityTypes.ActivityTypeCode,ActivityTypes.Description,"
        sql += "  BudgetDetails.NewQuantity ,BudgetDetails.ReplaceQuantity ,(BudgetDetails.NewQuantity+BudgetDetails.ReplaceQuantity )as totalQuantity,"
        sql += "  BudgetDetails.PricePerUnit, BudgetDetails.Amount"
        sql += " ,case when BudgetDetails.project_projectcode is null then 'งบประมาณประจำ' else 'งบประมาณโครงการ' end as project"

        sql += "   from ActivityTypes, BudgetDetails, FctParent "



        sql += "  where ActivityTypes.ActivityTypeCode = BudgetDetails.ActivityType_ActivityTypeCode"
        sql += "  and FctParent.FundCenterCode =BudgetDetails.FundCenter_FundCenterCode"




        sql += " and BudgetDetails.PlanYear  = " & Session("year") & " and BudgetDetails.Version = '" & Session("version") & "'"

        sql += " and ActivityTypes.PlanYear = " & Session("year")
        sql += " and FctParent.planyear = " & Session("year")


        If Session("FCTRmode") = "1" Then
            sql += " and  BudgetDetails.FundCenter_FundCenterCode IN (" & Session("FundcenterAll") & ") "
        ElseIf Session("FCTRmode") = "2" Then
            sql += " and  BudgetDetails.FundCenter_FundCenterCode IN (" & Session("FundcenterAll") & ") "
        End If

        If Val(Session("ActivityTypeS")) <> 0 And Val(Session("ActivityTypeE")) <> 0 Then
            sql += "   and  BudgetDetails.ActivityType_ActivityTypeCode between '" & Session("ActivityTypeS") & "' And '" & Session("ActivityTypeE") & "'"
        End If





        'Response.Write(sql)
        'Exit Function
        adapter.SearchReport(dt, sql)



        'Return 1
        'Exit Function
        dt.TableName = "R06DT1"
        Dim das As New DataSet
        das.DataSetName = "R01DS"
        das.Tables.Add(dt)
        ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local
        'ReportViewer1.LocalReport.ReportPath = System.Environment.CurrentDirectory & "Report\R01.rdlc"
        ReportViewer1.LocalReport.DataSources.Clear()
        ReportViewer1.LocalReport.DataSources.Add(New Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", das.Tables("R06DT1")))
        ReportViewer1.DocumentMapCollapsed = True

        Dim UserName As String = Session("users")
        Dim years As String = Session("year")
        Dim version As String = Session("version")
        Dim users As String = Session("users")

        Dim p1, p2, p3 As ReportParameter
        p1 = New ReportParameter("year", years)
        p2 = New ReportParameter("version", version)
        p3 = New ReportParameter("username", UserName)

        ReportViewer1.LocalReport.SetParameters(p1)
        ReportViewer1.LocalReport.SetParameters(p2)
        ReportViewer1.LocalReport.SetParameters(p3)


        ReportViewer1.LocalReport.Refresh()


        'Response.Write(dt.Rows.Count & sql)
        Session("count") = dt.Rows.Count



        Return dt.Rows.Count

    End Function


    Public Function nPageNumber() As String
        Dim str As String
        str = Me.Report.Globals!PageNumber.ToString()
        Return str
    End Function

End Class
